﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Serviio.API;

namespace Serviio.WHS
{
    public partial class DeviceDetailsForm : Form
    {
        private static readonly LogFacade log = LogFacade.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public DeviceDetailsForm()
        {
            InitializeComponent();
        }

        public ServiioDevice New(ServiioConnection connection)
        {
            Text = "Add New Device";
            textBoxName.ReadOnly = false;
            textBoxName.TabStop = true;
            textBoxName.BackColor = SystemColors.Window;
            textBoxIpAddress.ReadOnly = false;
            textBoxIpAddress.TabStop = true;
            textBoxIpAddress.BackColor = SystemColors.Window;

            LoadProfiles(connection);
            comboBoxProfile.SelectedIndex = 0;
            if (this.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return new ServiioDevice(connection, textBoxIpAddress.Text, textBoxName.Text, comboBoxProfile.SelectedValue.ToString());
            }
            return null;
        }

        public bool Edit(ServiioConnection connection, ServiioDevice device)
        {
            Text = "Edit Device Details";

            LoadProfiles(connection);
            textBoxIpAddress.Text = device.IpAddress;
            textBoxName.Text = device.Name;
            comboBoxProfile.SelectedValue = device.ProfileId;
            if (this.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                device.ProfileId = comboBoxProfile.SelectedValue.ToString();
                return true;
            }
            return false;
        }

        private void LoadProfiles(ServiioConnection connection)
        {
            comboBoxProfile.DataSource = new BindingSource(connection.Reference.Profiles, null);
            comboBoxProfile.DisplayMember = "Value";
            comboBoxProfile.ValueMember = "Key";
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
